package com.example.crudstudy.core.domain;

import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import lombok.Data;

/**
 * http响应结果封装
 *
 * @author fengshuonan
 * @date 2020/10/17 17:33
 */
@Data
public class ResponseData<T> {

    /**
     * 请求是否成功
     */
    @ChineseDescription("请求是否成功")
    private Boolean success;

    /**
     * 响应状态码
     */
    @ChineseDescription("响应状态码")
    private String code;

    /**
     * 响应信息
     */
    @ChineseDescription("响应信息")
    private String message;

    /**
     * 响应对象
     */
    @ChineseDescription("响应对象")
    private T data;

    public ResponseData() {
    }

    public static <Q> ResponseData<Q> success(Q data) {
        return new ResponseData<>(true, RuleConstants.SUCCESS_CODE, "操作成功", data);
    }

    public static  ResponseData<String> success(String msg) {
        return new ResponseData<>(true, RuleConstants.SUCCESS_CODE, msg);
    }


    /*
    *
    * RuleConstants.SUCCESS_CODE, RuleConstants.SUCCESS_MESSAGE,
    * */
    public static  ResponseData<String> success() {
        return new ResponseData<String>(true, RuleConstants.SUCCESS_CODE, "操作成功");
    }

    public static  ResponseData<Boolean> success(Boolean data) {
        if (data){
            return new ResponseData<>(true, RuleConstants.SUCCESS_CODE, "操作成功");
        }else {
            return new ResponseData<>(true, RuleConstants.BUSINESS_ERROR_TYPE_CODE, "操作失败");
        }

    }

    public static  ResponseData<Boolean> success(Boolean data,String message) {
            return new ResponseData<>(true, RuleConstants.SUCCESS_CODE, message, data);
    }

    public static <Q> ResponseData<Q> success(String msg, Q data) {
        return new ResponseData<>(true, RuleConstants.SUCCESS_CODE, msg, data);
    }

    public static <Q> ResponseData<Q> error() {
        return new ResponseData<>(false, RuleConstants.BUSINESS_ERROR_TYPE_CODE, "操作失败");
    }

    public static <Q> ResponseData<Q> error(String msg) {
        return new ResponseData<>(false, RuleConstants.BUSINESS_ERROR_TYPE_CODE, msg);
    }

    public static <Q> ResponseData<Q> error(String msg, Q data) {
        return new ResponseData<>(false, RuleConstants.BUSINESS_ERROR_TYPE_CODE, msg, data);
    }

    public ResponseData(Boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseData(Boolean success, String code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

}
